package cn.b.sky.mes.pp.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.b.sky.mes.pp.model.ProductionConfirmDetail;
import cn.b.sky.mes.pp.model.ProductionControl;
import cn.b.sky.mes.pp.model.condition.ProductionConfirmDetailCondition;
import cn.b.sky.mes.pp.model.condition.ProductionControlStatesCondition;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.file.ExcelUtil;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;
import cn.b.sky.mes.pp.service.OrderTypeService;
import cn.b.sky.mes.pp.service.ProductionConfirmDetailService;
import cn.b.sky.mes.pp.service.ProductionControlService;

import com.alibaba.fastjson.JSON;

/**
 * <strong>Title : ProductionControlStatusController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.pp.controller<br></strong>
 * <strong>Description : </strong>生产管理号执行状态分析控制器<br> 
 * <strong>Create on : 2016-5-19 下午1:10:32<br></strong>
 * <p>
 * @author JinQuan.Fu<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "productionControlStatus")
@Slf4j
public class ProductionControlStatusController extends SecurityController {

  @Resource
  ECodeService eCodeService;

  @Resource
  OrderTypeService orderTypeService;

  @Resource
  ProductionControlService productionControlService;

  @Resource
  ProductionConfirmDetailService productionConfirmDetailService;

  @RequestMapping("productionControlStatusList")
  public String productionControlStatusList(String navTabId,
                                            @ModelAttribute("condition") ProductionControlStatesCondition condition, Model model) {
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.PRODUCTIONCONTROL_STATUS.getCode(),
        SecurityContextUtil.getLanguageCode());
    condition.setSiteCode(this.getSiteCode());
    if (StringUtils.isEmpty(condition.getMinRequirementDate()) || StringUtils.isEmpty(condition.getMaxRequirementDate())) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      condition.setMaxRequirementDate(sdf.format(new Date()));
      Date date = new Date();
      date.setTime(date.getTime() - 7 * 24 * 60 * 60 * 1000);
      condition.setMinRequirementDate(sdf.format(date));
    }
    Map ControlStatus = productionControlService.productionControlStatusList(condition);
    String ControlStatusJSON = JSON.toJSONString(ControlStatus);
    model.addAttribute("controlStatusCode", ecode);
    model.addAttribute("controlStatusMap", ControlStatus);
    model.addAttribute("controlStatusJSON", ControlStatusJSON);
    model.addAttribute(NAVTABID, navTabId);
    return "pp/productionControlStatus/productionControlStatus_list";
  }

  @RequestMapping(value = "viewProductionControlByStatus")
  public String viewProductionOrdersByStatus(@ModelAttribute("condition") ProductionControlStatesCondition condition,
      Model model, String status) {
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.PRODUCTIONCONTROL_STATUS.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("ecodes", JSON.toJSONString(ecode));
    model.addAttribute("condition", JSON.toJSONString(condition));
    model.addAttribute("status", status);
    return "pp/productionControlStatus/groupProductionControl_list";
  }

  @RequestMapping(value = "getProductionControlsByStatus")
  @ResponseBody
  public DWZResult getProductionControlsByStatus(String status,
      @ModelAttribute("condition") ProductionControlStatesCondition condition) {
    condition.setSiteCode(getSiteCode());
    List<ProductionControl> list;
    int count;
    if (!StringUtils.isBlank(status) && !StringUtils.isBlank(condition.getStatuseCode())
        && !status.equals(condition.getStatuseCode())) {
      list = new ArrayList<>();
      count = 0;
    } else {
      list = productionControlService.selectproductionControlStatusBycondition(status, condition);
      count = productionControlService.selectProductionControlStatusCountBycondition(condition);
    }
    condition.setData(list);
    condition.setTotalCount(count);
    return ajaxDone(condition);
  }

  @RequestMapping(value = "getControlConfirmByControlNum")
  @ResponseBody
  public DWZResult getControlConfirmByControlNum(String controlNum) {
    ProductionConfirmDetailCondition condition = new ProductionConfirmDetailCondition();
    condition.setSiteCode(getSiteCode());
    condition.setProductionControlNum(controlNum);
    List<ProductionConfirmDetail> prodcutionOrderDetail = productionConfirmDetailService
        .selectSubmitDetailList(condition);
    int count = productionConfirmDetailService.getSubmitDetailCount(condition);
    condition.setData(prodcutionOrderDetail);
    condition.setTotalCount(count);
    return ajaxDone(condition);
  }

  @RequestMapping(value = "exportControlsToExcel")
  public void exportControlsToExcel(HttpServletResponse response, String statusECode,
      @ModelAttribute("condition") ProductionControlStatesCondition condition) {
    condition.setSiteCode(getSiteCode());
    List<ProductionControl> list = productionControlService.selectproductionControlStatusBycondition(statusECode, condition);
    List<ECode> ecodes = eCodeService.selectECodeList(ECodeEnums.PRODUCTIONCONTROL_STATUS.getCode(),
        SecurityContextUtil.getLanguageCode());
    for (int i = 0; i < list.size(); i++) {
      for (ECode ecode : ecodes) {
        if (ecode.geteCode().equals(list.get(i).getStatuseCode())) {
          list.get(i).setStatuseText(ecode.geteCodeText());
        }
      }
    }
    String languageCode = SecurityContextUtil.getLanguageCode();
    String productionControlReport = getImeViewMessageI18n(languageCode, "productionControlReport.C");
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("productionControlNum", getImeViewMessageI18n(languageCode, "productionControlNum.C"));
    map.put("statuseText", getImeViewMessageI18n(languageCode, "orderStatusECode.C"));
    map.put("workCenterCode", getImeViewMessageI18n(languageCode, "workCenter.C"));
    map.put("workCenterText", getImeViewMessageI18n(languageCode, "workCenterText.C"));
    map.put("materialCode", getImeViewMessageI18n(languageCode, "materialCode.C"));
    map.put("materialText", getImeViewMessageI18n(languageCode, "materialText.C"));
    map.put("plannedQuantity", getImeViewMessageI18n(languageCode, "plannedQuantity.C"));
    map.put("completedQuantity", getImeViewMessageI18n(languageCode, "completedQuantity.C"));
    map.put("scrappedQuantity", getImeViewMessageI18n(languageCode, "scrappedQuantity.C"));
    map.put("repairQuantity", getImeViewMessageI18n(languageCode, "repairQuantity.C"));
    map.put("actualstartDateTime", getImeViewMessageI18n(languageCode, "orderStatusECode.C"));
    map.put("actualendDateTime", getImeViewMessageI18n(languageCode, "orderStatusECode.C"));
    map.put("processCode", getImeViewMessageI18n(languageCode, "cmprocess_code.C"));
    try {
      ExcelUtil.exportToExcel(response, productionControlReport, map, list);
    } catch (IOException e) {
      log.error("{}", e);
    }
  }

  @RequestMapping(value = "exportconfirmToExcel")
  public void exportconfirmToExcel(String productionControlNum, HttpServletResponse response) {
    ProductionConfirmDetailCondition condition = new ProductionConfirmDetailCondition();
    condition.setSiteCode(getSiteCode());
    condition.setProductionControlNum(productionControlNum);
    List<ProductionConfirmDetail> prodcutionOrderDetail = productionConfirmDetailService
        .selectSubmitDetailList(condition);
    String languageCode = SecurityContextUtil.getLanguageCode();
    String productionControlReport = getImeViewMessageI18n(languageCode, "productionControlConfirmReport.C");
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("productionControlNum", getImeViewMessageI18n(languageCode, "productionControlNum.C"));
    map.put("confirmDateTime", getImeViewMessageI18n(languageCode, "confirmDateTime.C"));
    map.put("confirmUser", getImeViewMessageI18n(languageCode, "confirmUser.C"));
    map.put("completedQuantity", getImeViewMessageI18n(languageCode, "completedQuantity.C"));
    map.put("scrappedQuantity", getImeViewMessageI18n(languageCode, "scrappedQuantity.C"));
    try {
      ExcelUtil.exportToExcel(response, productionControlReport, map, prodcutionOrderDetail);
    } catch (IOException e) {
      log.error("{}", e);
    }
  }
}
